import { message } from 'ant-design-vue';
import { encryptionToString } from "@/util/qetag";
import { filterSize } from "@/util/fileSize"
import { Notify } from '@/util/notify';
import { location } from "@/util/tool";
/**
 * @param { notAcceptType: array, file: file, isHash: Boolean, maxSize: Number }
 *  
 */
let id = 1000;
export const Upload = {
  // 检查文件基本内容 并返回文件hash值
  async filterContentReturnHash(notAcceptType, file, isHash, maxSize = 10485760) {
    // 检查文件类型
    if (
      notAcceptType.indexOf(file.type) !== -1 || notAcceptType.indexOf(file.name.substring(file.name.lastIndexOf("."))) !== -1) {
      message.error({
        content: "此类型文件无法上传！",
      });
      return false;
    }

    // 检查文件大小
    if (maxSize && file.size > maxSize) {
      message.error({
        content: `上传文件最大为${filterSize(maxSize)}！`,
      });
      return false;
    }

    // 文件名
    let fileName = file.name;
    // 过滤英文分号
    fileName = this.filterEnSplit(file.name)

    // 返回的数据
    const data = { fileName: fileName }
    if (isHash) {
      data.hash = await this.hashFile(file)
    }
    return data
  },
  // hash文件内容
  async hashFile(file) {
    let hash = await encryptionToString(file);
    if (hash === "Fto5o-5ea0sNMlW_75VgGJCv2AcJ") {
      message.error({
        content: "文件读取错误！",
      });
      return false;
    } else return hash
  },
  // 过滤英文分号
  filterEnSplit(name) {
    if (name.indexOf(";") !== -1) {
      return name.replace(/;/g, "；");
    } else return name
  },

  NotifyObj: {
    type: "info",
    message: '<div><i class="el-icon-loading"></i><span>文件上传中</span></div>',
    time: 0,
    dangerouslyUseHTMLString: true,
  },

  // 创建上传成功通知消息
  uploadSuccessNotify(prevNotifyEl, time = 1500) {
    prevNotifyEl && Notify.remove(prevNotifyEl);
    return Notify.create("success", "文件上传成功!", time);
  },

  // 创建上传失败通知消息
  uploadErrorNotify(prevNotifyEl, message = "文件上传失败!", time = 1500) {
    prevNotifyEl && Notify.remove(prevNotifyEl);
    return Notify.create("error", message, time);
  },

  // 移除消息
  removeNotify(prevNotifyEl) {
    prevNotifyEl && Notify.remove(prevNotifyEl);
  },

  // 删除host地址
  deleteHttpLocation(data) {
    data.forEach(item => {
      let url = item.fileUrl || "/" + item.fileName
      if (!url) return
      var site = url.lastIndexOf("/");
      //截取最后一个/后的值
      item.fileUrl = url.substring(site, url.length);
      item.id && delete item.id
      if (item.subFiles?.length) {
        return this.deleteHttpLocation(item.subFiles)
      }
    })
  },

  // 添加http:// 地址
  addHttpLocation(data, parentPath) {
    if (parentPath && [".rar",".zip",".RAR",".ZIP"].indexOf(parentPath.substring(parentPath.lastIndexOf('.'))) !== -1) parentPath = parentPath.replace(".rar", "").replace(".zip", "").replace(".RAR", "").replace(".ZIP", "")
    data.forEach(item => {
      if (parentPath) item.fileUrl = parentPath + (item.fileUrl || '/' + item.fileName)
      item.id = id++
      if (item.subFiles?.length) {
        return this.addHttpLocation(item.subFiles, item.fileUrl)
      }
    })
    return data
  },

  // 将文件名转为路径
  fileNameSetToFileUrl(arr) {
    let subArr = []
    arr.forEach(item => {
      if (!item.fileUrl) item.fileUrl = "/" + item.fileName
      if (item.subFiles?.length) {
        this.fileNameSetToFileUrl(item.subFiles)
      }
      subArr.push(item)
    })
    return subArr
  }
}